{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.8. Writing unit tests with py.test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting first.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile first.py\n",
    "def first(l):\n",
    "    return l[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Appending to first.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile -a first.py\n",
    "\n",
    "# This is appended to the file.\n",
    "def test_first():\n",
    "    assert first([1, 2, 3]) == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "def first(l):\n",
      "    return l[0]\n",
      "\n",
      "# This is appended to the file.\n",
      "def test_first():\n",
      "    assert first([1, 2, 3]) == 1\n"
     ]
    }
   ],
   "source": [
    "%cat first.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "============= test session starts ==============\n",
      "platform linux -- Python 3.6.3, pytest-3.2.1, py-1.4.34\n",
      "rootdir: ~/git/cookbook-2nd/chapter02_best_practices:\n",
      "plugins: cov-2.5.1\n",
      "\n",
      "collecting 0 items\n",
      "collecting 1 item\n",
      "collected 1 item\n",
      "\n",
      "first.py .\n",
      "\n",
      "=========== 1 passed in 0.00 seconds ===========\n"
     ]
    }
   ],
   "source": [
    "!pytest first.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting first.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile first.py\n",
    "def first(l):\n",
    "    return l[0]\n",
    "\n",
    "def test_first():\n",
    "    assert first([1, 2, 3]) == 1\n",
    "    assert first([]) is None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "============= test session starts ==============\n",
      "platform linux -- Python 3.6.3, pytest-3.2.1, py-1.4.34\n",
      "rootdir: ~/git/cookbook-2nd/chapter02_best_practices:\n",
      "plugins: cov-2.5.1\n",
      "\n",
      "collecting 0 items\n",
      "collecting 1 item\n",
      "collected 1 item\n",
      "\n",
      "first.py F\n",
      "\n",
      "=================== FAILURES ===================\n",
      "__________________ test_first __________________\n",
      "\n",
      "    def test_first():\n",
      "        assert first([1, 2, 3]) == 1\n",
      ">       assert first([]) is None\n",
      "\n",
      "first.py:6:\n",
      " _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _\n",
      "\n",
      "l = []\n",
      "\n",
      "    def first(l):\n",
      ">       return l[0]\n",
      "E       IndexError: list index out of range\n",
      "\n",
      "first.py:2: IndexError\n",
      "=========== 1 failed in 0.02 seconds ===========\n"
     ]
    }
   ],
   "source": [
    "!pytest first.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting first.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile first.py\n",
    "def first(l):\n",
    "    return l[0] if l else None\n",
    "\n",
    "def test_first():\n",
    "    assert first([1, 2, 3]) == 1\n",
    "    assert first([]) is None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "============= test session starts ==============\n",
      "platform linux -- Python 3.6.3, pytest-3.2.1, py-1.4.34\n",
      "rootdir: ~/git/cookbook-2nd/chapter02_best_practices:\n",
      "plugins: cov-2.5.1\n",
      "\n",
      "collecting 0 items\n",
      "collecting 1 item\n",
      "collected 1 item\n",
      "\n",
      "first.py .\n",
      "\n",
      "=========== 1 passed in 0.00 seconds ===========\n"
     ]
    }
   ],
   "source": [
    "!pytest first.py"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
